﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Fayshine.VA.Helpers;
using Fayshine.VA.Models;
using Fayshine.VA.RIAService.Web;
using Fayshine.VA.ViewModels;
using Visifire.Charts;
using LocalBusyIndicator = Fayshine.VA.Controls.BusyIndicator;

namespace Fayshine.VA.Views.SearchStatistics.Year
{
    public partial class YearMonthStatisticsCompare : Page
    {
        private StatisticsReportViewModel m_ViewModel;

        public YearMonthStatisticsCompare()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(YearMonthStatisticsCompare_Loaded);
        }

        private void YearMonthStatisticsCompare_Loaded(object sender, RoutedEventArgs e)
        {
            LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
            indicator.IsBusy = true;

            cmbYear.ItemsSource = FayshineManager.Instance.GetYearSource();
            cmbYear.SelectedValue = DateTime.Now.Year;

            m_ViewModel = new StatisticsReportViewModel();
            m_ViewModel.ResetHisotryChartEvent += new ResetHistoryChartEventHandler(m_ViewModel_ResetHisotryChartEvent);
            m_ViewModel.AppendHistoryPassengerFlow += new AppendHistoryPassengerFlowEventHandler(m_ViewModel_AppendHistoryPassengerFlow);
            this.DataContext = m_ViewModel;

            m_ViewModel.GetAllAreas();
        }

        void m_ViewModel_AppendHistoryPassengerFlow(string title, bool isIn, List<HistoryPassengerFlow> firstHistoryPassengerFlow, List<HistoryPassengerFlow> secondHistoryPassengerFlow)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                
                Axis xAxis = new Axis();

                vcChart1.AxesX.Add(xAxis);
                
                Title vcChart1Title = new Visifire.Charts.Title();
                vcChart1Title.Text = title;
                vcChart1.Titles.Add(vcChart1Title);
                RenderAs render = cmbStyle.SelectedIndex == 0 ? RenderAs.Line : RenderAs.Column;
                if (cmbInorOut.SelectedIndex == 0)
                {
                    DataSeries series = ChartHelper.GetDataSeries(render, "进客流", ChartValueTypes.Auto);
                    series.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                    series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "InCount"));
                    series.DataSource = firstHistoryPassengerFlow;
                    vcChart1.Series.Add(series);
                }
                else if (cmbInorOut.SelectedIndex == 1)
                {
                    DataSeries series = ChartHelper.GetDataSeries(render, "出客流", ChartValueTypes.Auto);
                    series.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                    series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "OutCount"));
                    series.DataSource = firstHistoryPassengerFlow;
                    vcChart1.Series.Add(series);
                }
                else
                {
                    DataSeries series = ChartHelper.GetDataSeries(render, "进客流", ChartValueTypes.Auto);
                    series.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                    series.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "InCount"));
                    series.DataSource = firstHistoryPassengerFlow;
                    vcChart1.Series.Add(series);

                    DataSeries series2 = ChartHelper.GetDataSeries(render, "出客流", ChartValueTypes.Auto);
                    series2.DataMappings.Add(ChartHelper.GetDataMapping("AxisXLabel", "DisplayTitle"));
                    series2.DataMappings.Add(ChartHelper.GetDataMapping("YValue", "OutCount"));
                    series2.DataSource = firstHistoryPassengerFlow;
                    vcChart1.Series.Add(series2);
                }

                this.dataGrid.ItemsSource = firstHistoryPassengerFlow;

            });
        }

        private void m_ViewModel_ResetHisotryChartEvent()
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                if (vcChart1 != null)
                {
                    vcChart1.AxesX.Clear();
                    vcChart1.TrendLines.Clear();
                    vcChart1.Series.Clear();
                    vcChart1.Titles.Clear();
                }
            });
        }

        private void cmbAreas_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                m_ViewModel.AreaExtends.each(a => a.IsSelected = false);

                var selectItem = cmbAreas.SelectedItem as AreaExtend;
                if (selectItem != null)
                {
                    selectItem.IsSelected = true;
                }

                List<area> selectedArea = new List<area>();
                var selectAreaExt = m_ViewModel.AreaExtends.Where(a => a.IsSelected).ToList();
                selectAreaExt.ForEach(b =>
                {
                    area temp = b.Tag as area;
                    selectedArea.Add(temp);
                });

                m_ViewModel.SelectAreasChanged(selectedArea);
                cmbPassenger_DropDownClosed(null, null);
            }
        }

        private void cmbPassenger_DropDownClosed(object sender, EventArgs e)
        {
            if (m_ViewModel != null)
            {
                var selectMonitorAreaExts = m_ViewModel.MonitorAreaGateExts.Where(a => a.IsSelected).ToList();

                if (selectMonitorAreaExts.Count > 0)
                {
                    cbItemDisplay2.Content = selectMonitorAreaExts.Count > 1 ? selectMonitorAreaExts[0].Name + "..." : selectMonitorAreaExts[0].Name;

                }
                else
                {
                    cbItemDisplay2.Content = string.Empty;
                }
                cmbPassenger.SelectedItem = null;
                cmbPassenger.SelectedItem = cbItemDisplay2;
            }
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string msg = string.Empty;
            
            m_ViewModel.UpdateCheckedAreas();

            if (!FayshineManager.Instance.ValidateAreaGateInputs(m_ViewModel.CheckedAreas,
                m_ViewModel.CheckedGates, ref msg))
            {
                MessageBox.Show(msg);
            }
            else
            {
                DateTime dateTime = new DateTime((int)cmbYear.SelectedValue, 1, 1);

                m_ViewModel.RefreshYearReport(true, dateTime);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog fsd = new SaveFileDialog();
            fsd.Filter = "PDF (*.pdf)|*.pdf|*PNG (*.png)|*.png";

            if (fsd.ShowDialog() == true)
            {
                LocalBusyIndicator indicator = Application.Current.RootVisual as LocalBusyIndicator;
                indicator.IsBusy = true;
                this.Dispatcher.BeginInvoke(delegate()
                {
                    FayshineManager.Instance.ExportToDisk(fsd, spContent);
                });
            }
        }

    }
}
